package com.bolovan.infovaluta.action;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;

import com.bolovan.infovaluta.bean.ExchangeRate;
import com.bolovan.infovaluta.bean.ExchangeRateEntry;
import com.bolovan.infovaluta.db.PMF;
import com.bolovan.infovaluta.util.RateMaps;

@UrlBinding("/api/store.action")
public class StoreMaps extends BaseAction {
	@DefaultHandler
	public Resolution store() {
		List<ExchangeRate> maps = new ArrayList<>();
		maps.add(RateMaps.bnr);
		maps.add(RateMaps.bce);
		maps.add(RateMaps.bcr);
		maps.add(RateMaps.bt);
		maps.add(RateMaps.brd);
		maps.add(RateMaps.ing);
		maps.add(RateMaps.raiffeisen);

		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			for (ExchangeRate r : maps) {
				for (ExchangeRateEntry e : r.getCurrencyMap()) {
					pm.makePersistent(e);
				}
			}
			Query q2 = pm.newQuery(ExchangeRateEntry.class);

			System.out.println("ExchangeRateEntry");
			List<ExchangeRateEntry> results2 = (List<ExchangeRateEntry>) q2.execute();
			if (!results2.isEmpty()) {
				for (ExchangeRateEntry p2 : results2) {
					System.out.println(p2);
				}
			}

		} finally {
			pm.close();
		}
		return new StreamingResolution("json", "{respons:{'status':'success'}");

	}
}
